package cn.ydt.ydt.community.service.impl;

import cn.ydt.ydt.common.exception.YdtServiceException;
import cn.ydt.ydt.common.resful.JsonResult;
import cn.ydt.ydt.common.resful.StatusCode;
import cn.ydt.ydt.community.common.restful.JsonPage;
import cn.ydt.ydt.community.common.utils.DistanceUtils;
import cn.ydt.ydt.community.common.utils.SecurityContextUtils;
import cn.ydt.ydt.community.mapper.EnclosureSingInLocationMapper;
import cn.ydt.ydt.community.mapper.SingInMapper;
import cn.ydt.ydt.community.pojo.dto.ModifySingInInfoDTO;
import cn.ydt.ydt.community.pojo.vo.EnclosureSIngInLocationVO;
import cn.ydt.ydt.community.pojo.vo.SignInfoVO;
import cn.ydt.ydt.community.service.SingInService;
import cn.ydt.ydt.community.pojo.dto.SingInDTO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.security.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
@Slf4j(topic = "community.sing-in")
public class SingInServiceImpl implements SingInService {
    private static final String IN_CLOSURE = "签到成功!";
    private static final String OUT_CLOSURE= "签到成功，未在签到范围内!";
    @Autowired
    SingInMapper singInMapper;
    @Autowired
    EnclosureSingInLocationMapper enclosureMapper;

    @Override
    public String  addSingInRecord(SingInDTO singInDTO) {
        //TODO 社区id测试值为1L，待更改为SecurityContextUtils.getUserInfo().getCommunityId()
        //获得该社区的打卡区域信息
        log.info("社区ID={}",SecurityContextUtils.getUserInfo().getCommunityId());
        EnclosureSIngInLocationVO enclosure = enclosureMapper.getEnclosureInfoByCommunityId(SecurityContextUtils.getUserInfo().getCommunityId());
        //计算打卡点和中心点的位置
        singInDTO.setUserId(SecurityContextUtils.getUserId());
        singInDTO.setCommunityId(SecurityContextUtils.getUserInfo().getCommunityId());

        double lineDistance = DistanceUtils.getLineDistance(enclosure.getCenterLatitude(), enclosure.getCenterLongitude()
                ,  singInDTO.getLatitude(), singInDTO.getLongitude());
        log.info("签到范围半径:{}   签到位置距离：{}" , enclosure.getRadius() , lineDistance);
        //判断二者距离是否大于打卡范围半径
        int isWithRange = (enclosure.getRadius() <= lineDistance)?1:0;
        singInDTO.setIsWithinRange(isWithRange);
        String message = (enclosure.getRadius() <= lineDistance)?OUT_CLOSURE:IN_CLOSURE;
        //将打卡信息录入数据库
        if (singInMapper.addSingInRecord(singInDTO) < 1){
            throw new YdtServiceException(StatusCode.BAD_REQUEST , "签到失败，请稍后再试！");
        }
        return  message;

    }

    @Override
    public JsonPage<SignInfoVO> getCurrentSignInInfoByCommunityId(Integer page  , Integer pageSize, Integer difference) {

        PageHelper.startPage(page , pageSize);
        //TODO 社区id测试值为1L，待更改为SecurityContextUtils.getUserInfo().getCommunityId()
        List<SignInfoVO> signInfo = singInMapper.getCurrentSignInInfoByCommunityId(SecurityContextUtils.getNetCommunityId(), difference);
        //设置分页格式
        PageHelper.startPage(page , pageSize);
        //返回JsonPage
        return JsonPage.restPage(new PageInfo<>(signInfo));
    }

    @Override
    public JsonPage<SignInfoVO> getPersonSignInInfo(Integer page  , Integer pageSize) {
        Long userId = SecurityContextUtils.getUserId();
        Long accUserId = (userId == 0)?SecurityContextUtils.getUserId():userId;
        PageHelper.startPage(page , pageSize);
        List<SignInfoVO> signInfo =   singInMapper.getSignInInfoByUserId(accUserId);
        //设置分页格式

        //返回JsonPage
        return JsonPage.restPage(new PageInfo<>(signInfo));
    }

    @Override
    public  void modifySingInInfo(ModifySingInInfoDTO modify){
        singInMapper.modifySingInInfo(modify);

    }

}
